package com.cxl.price.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {
	

	String filePath="C:\\Documents and Settings\\Administrator\\桌面\\图片\\";
	
	
	
	public   String   saveToFile(String urlImg,String fileName) throws Exception   { 
		URL   url   =   new   URL(urlImg); 
        URLConnection   uc   =   url.openConnection(); 
        InputStream   is   =   uc.getInputStream(); 
        File   file   =   new   File(filePath+fileName+".jpg"); 
        FileOutputStream   out   =   new   FileOutputStream(file); 
        int   i=0; 
        while   ((i=is.read())!=-1)   { 
                out.write(i); 
        } 
        is.close();
        
        return filePath+fileName;
    }   
	
	public void deleteFile(String file)
	{
		File ff=new File(filePath+file);
		if(ff.exists())
		{
			ff.delete();
		}
	}
	
	
	
	public   void   saveToTbi(String urlImg,String fileName) throws Exception   { 
        InputStream   is   =   new FileInputStream(new File(filePath+urlImg+".jpg"));
        File   file   =   new   File(filePath+fileName+".tbi"); 
        FileOutputStream   out   =   new   FileOutputStream(file); 
        int   i=0; 
        while   ((i=is.read())!=-1)   { 
                out.write(i); 
        } 
        is.close();
    }   
	
	/**
	 * 保存网络图片成tbi
	 * @param urlImg
	 * @param fileName
	 * @throws Exception
	 */
	
	public   void   saveOtherImgToTbi(String urlImg,String fileName) throws Exception   { 
		URL   url   =   new   URL(urlImg); 
        URLConnection   uc   =   url.openConnection(); 
        InputStream   is   =   uc.getInputStream(); 
        File   file   =   new   File(filePath+fileName+".tbi"); 
        FileOutputStream   out   =   new   FileOutputStream(file); 
        int   i=0; 
        while   ((i=is.read())!=-1)   { 
                out.write(i); 
        } 
        is.close();
    }   
	
	

	public  File getImageFile(String title)
	{
		return new File(filePath+title+".jpg");
	}
	
	public static byte[] getHttpImgBytes(String urlImg) throws Exception   
	{
			URL   url   =   new   URL(urlImg); 
	        URLConnection   uc   =   url.openConnection(); 
	        InputStream   is   =   uc.getInputStream(); 
	        byte[] img = new byte[1024*300];  

	        is.read(img)	;      
	        is.close();
	        return img;
	}   
		
	
	/**
	* 图片水印
	* @param pressimg 水印图片
	* @param targetimg 目标图片
	* @param location 位置：1、左上角，2、右上角，3、左下角，4、右下角，5、正中间
	* @param alpha 透明度
	*/
	public  void pressImage(String pressimg, String targetimg, int location, float alpha) {
		try {
		//读取目标图片
		File img = new File(filePath+targetimg);
		Image src = ImageIO.read(img);
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, width, height, null);
		//水印文件
		Image src_biao = ImageIO.read(new File(filePath+pressimg));
		int width_biao = src_biao.getWidth(null);
		int height_biao = src_biao.getHeight(null);
		//如果水印图片高或者宽大于目标图片是做的处理,使其水印宽或高等于目标图片的宽高，并且等比例缩放
		int new_width_biao = width_biao;
		int new_height_biao = height_biao;
		if(width_biao > width){
		new_width_biao = width;
		new_height_biao = (int) ((double)new_width_biao/width_biao*height);
		}
		if(new_height_biao > height){
		new_height_biao = height;
		new_width_biao = (int) ((double)new_height_biao/height_biao*new_width_biao);
		}
		//根据位置参数确定坐标位置
		int x = 0;
		int y = 0;
		switch(location)
		{
		case 1:
		break;
		case 2:x = width - new_width_biao;
		break;
		case 3:y = height - new_height_biao;
		break;
		case 4:
			x = width - new_width_biao;
		y = height - new_height_biao;
		break;
		case 5:
		x = (width - new_width_biao)/2;
		y = (height - new_height_biao)/2;
		break;
		default:
		break;
		}
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
		g.drawImage(src_biao, x, y, new_width_biao, new_height_biao, null);
		//水印文件结束
		g.dispose();
		ImageIO.write( image, "png", img);
		
		} catch (Exception e) {
		e.printStackTrace();
		}
		}
	
	public void addBorderToImage(String files) {
		  try {
		   File _file = new File(filePath+files); // 读入文件
		   Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
		   int width = src.getWidth(null); // 得到源图宽
		   int height = src.getHeight(null); // 得到源图长

		   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		   Graphics graphics = image.getGraphics();
		   graphics.drawImage(src, 0, 0, width, height, null); // 绘制图
		   // 画边框

		   graphics.setColor(Color.BLACK);
		   graphics.drawRect(0, 0, width - 1, height - 1);
		   graphics.drawRect(1, 1, width - 1, height - 1);
		   graphics.drawRect(0, 0, width-2, height- 2);  

		   FileOutputStream out = new FileOutputStream(filePath+files); // 输出到文件流
		   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		   encoder.encode(image);
		   out.close();
		  }
		  catch (IOException e) {
		   e.printStackTrace();
		  }
		 }


	
		
	public static void main(String[] args) throws Exception {
	//	getHttpImgBytes("http://img12.360buyimg.com/n1/1587/c0388a30-ae39-45bc-b051-dc697df6a485.jpg");
		new ImageUtil().pressImage("123.png","123.jpg", 1,1f );
		new ImageUtil().addBorderToImage("123.jpg");
	}

}
